package org.hsqldb.index;

import java.io.IOException;

import org.hsqldb.Row;
import org.hsqldb.RowBTreeDisk;
import org.hsqldb.RowLinkList;
import org.hsqldb.persist.PersistentStore;
import org.hsqldb.rowio.RowInputInterface;
import org.hsqldb.rowio.RowOutputInterface;

public class NodeBTreeDataDisk extends NodeBTreeData implements NodeBTreeDisk{

	public static int SIZE_IN_BYTE = 5 * 4;
	
	final RowBTreeDisk row;
	protected int parentNodeId = NO_POS;
	protected int indexPosition;
	protected int dataId; 
	protected int prevDataId = NO_POS;
	protected int nextDataId = NO_POS;
	
	public NodeBTreeDataDisk(RowBTreeDisk rowBTreeDisk, RowInputInterface in,
			int i) throws IOException {
		row = rowBTreeDisk;
		indexPosition = i;
		dataId = row.getPos();
		positionInParent = in.readInt();
		parentNodeId = in.readInt();
		prevDataId = in.readInt();
		nextDataId = in.readInt();
	}
	
	@Override
	public void write(RowOutputInterface out){
		out.writeInt(positionInParent);
		out.writeInt(parentNodeId);
		out.writeInt(prevDataId);
		out.writeInt(nextDataId);
	}
	
	public NodeBTreeDataDisk(RowBTreeDisk rowBTreeDisk, int i) {
		row = rowBTreeDisk;
		indexPosition = i;
		dataId = row.getPos();
	}

	
	@Override 
	public void setNodeChanged(){
		row.setNodesChanged();
	}
	public boolean isInMemory() {
		return row.isInMemory();
	}
	
	public int getPos() {
        return dataId;
    }

	public void setPos(int p){
		dataId = p;
	}
	@Override
	public void setPre(PersistentStore store,NodeBTreeData pre){
		nodePrev = pre;
		if(pre == null){
			prevDataId = NO_POS;
		}
		else {
			prevDataId = pre.getPos();
		}
		setNodeChanged();
	}
	
	@Override
	public void setNext(PersistentStore store,NodeBTreeData next){
		nodeNext = next;
		if(next == null){
			nextDataId = NO_POS;
		}
		else{
			nextDataId = next.getPos();
		}
		setNodeChanged();
	}
	
	@Override
	public NodeBTreeData getNext(PersistentStore store){
		if(nextDataId == NO_POS)
			return null;
		if(nodeNext != null)
			return nodeNext;
		return nodeNext = (NodeBTreeData) store.get(nextDataId);
	}
	
	@Override 
	public NodeBTreeData getPre(PersistentStore store){
		if(prevDataId == NO_POS)
			return null;
		if(nodePrev != null)
			return nodeNext;
		return (NodeBTreeData) store.get(prevDataId);
	}
	
	public NodeBTree getParent(PersistentStore store){
		if(parentNodeId == NO_POS)
			return null;
		if(nodeParent != null)
			return nodeParent;
		
		return nodeParent = (NodeBTree) store.get(parentNodeId);
	}
	@Override
	public void setParent(PersistentStore store,NodeBTree p,int pos){
		nodeParent = p;
		if(null == p){
			parentNodeId = NO_POS;
		}
		else {
			parentNodeId = p.getPos();
		}
		positionInParent = pos;
		setNodeChanged();
	}
	
	public Row getRow(PersistentStore store) {

        if (!row.isInMemory()) {
            return (RowBTreeDisk) store.get(this.row, false);
        } else {
            row.updateAccessCount(store.getAccessCount());
        }
        return row;
    }
}
